﻿using Assets.Scripts.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using DG.Tweening;
/// <summary>
/// 用作道具的获取和一些点击事件
/// </summary>
public class PropManager : Singleton<PropManager>
{
    #region field
    [SerializeField, Tooltip("玩家的道具")]
    private AProp[] _items = new AProp[3];
    [Tooltip("玩家的道具按钮")]
    public GameObject[] propButton;
    [SerializeField, Tooltip("获取设置的按钮")]
    private Button setButton;
    [Tooltip("获取一个Music播放器")]
    public ObtainMusic mmPlayer;
    [Tooltip("获取一个拾取道具播放器")]
    public ObtainMusic pvPlayer;
    #endregion
    public AProp[] Items
    {
        get
        {
            return _items;
        }
    }
   
    public override void FixedAwake()
    {
        Init_Button();
        GameObject go1 = GameObject.Find("Canvas/Set/BGMPlayer");
        if (go1 != null) mmPlayer = go1.GetComponent<ObtainMusic>();
        GameObject go2 = GameObject.Find("Canvas/PropVoice");
        if (go2 != null) pvPlayer = go2.GetComponent<ObtainMusic>();
        GameObject go3 = GameObject.Find("Canvas/GeneralSetting");
        if (go3 != null)
        {
            setButton = go3.GetComponent<Button>();
            setButton.onClick.AddListener(MusicSet);
        }
        Testing();
    }
    public void ChangeItem(AProp ap)
    {
        AProp before = _items[(int)ap.Type];
        if (before == null)//原来没有物品，直接赋给
        {
            _items[(int)ap.Type] = ap;
            if(propButton[(int)ap.Type]!=null)
            propButton[(int)ap.Type].transform.GetChild(2).GetComponent<Text>().text = ap.CountText();

        }
        else
        {
           // print(before.GetType().Name+","+this.GetType().Name);
            if (before.GetType() == ap.GetType())//如果是同一种武器，使用次数相加
            {
                
                //print(before.useCount);
                before.useCount +=ap.useCount;
                propButton[(int)ap.Type].transform.GetChild(2).GetComponent<Text>().text = ap.CountText();
                GameObject.Destroy(ap.gameObject);//销毁传入的AProp。因为已经存在同样的对象
            }
            else
            {
                //不是同一种物品则覆盖原来的
                GameObject.Destroy(before.gameObject);//销毁原来的物品
                _items[(int)ap.Type] = ap;
            }
        }
  
    }

    #region 打开音乐按钮事件
    public void MusicSet()
    {
        PlayS(0);
        Time.timeScale = 0;
        UIshower.Instance.StartPlay(Nodeal.SetMusic);
    }
    #endregion
    public void ReturnCp()
    {
        PlayS(1);
        LoadManager.Instance.Loading(1);
        SceneManager.LoadScene(1);
    }//返回选关界面
    public void Init_Button()
    {
        #region 隐初始化藏掉三个按钮
        propButton[0] = GameObject.Find("Canvas/PropButton/FunctButton");
        propButton[1] = GameObject.Find("Canvas/PropButton/AttackButton");
        propButton[2] = GameObject.Find("Canvas/PropButton/DefenseButton");

        for (int i = 0; i < 3; i++)
        {
            if (propButton[i] != null)
                propButton[i].SetActive(false);
        }
        #endregion

    }//自动获取按钮点击事件

    public void Restart()
    {
        PlayS(0);
        LoadManager.Instance.Loading(SceneManager.GetActiveScene().buildIndex);
        Time.timeScale = 1;
    }//重新开始

    private void PlayS(int index)
    {
        if (SetMManager.Instance != null)
            SetMManager.Instance.PlayMusic(index);
    }
    private void Testing()
    {
        for(int i=0;i<3;i++)
        {
            if(_items[i]!=null)
            {
                ChangeItem(_items[i]);
            }
        }
    }
}
